package cn.psvmc.cxzapi.controller.system;

import cn.psvmc.cxzapi.enums.ResultCode;
import cn.psvmc.cxzapi.model.common.DelEntity;
import cn.psvmc.cxzapi.model.common.SearchEntity;
import cn.psvmc.cxzapi.model.system.TUser;
import cn.psvmc.cxzapi.response.*;
import cn.psvmc.cxzapi.service.system.TUserService;
import cn.psvmc.cxzapi.util.PageUtil;
import cn.psvmc.cxzapi.util.ToolUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * 用户表相关操作
 * @Author auto
 * @Date 2024-08-08
 */
@RestController
@RequestMapping("/user")
public class TUserController {

    @Resource
    private TUserService tUserService;

    /**
    * @Author auto 2024-08-08 新增用户表
    * @Param tUser 用户表
    * @Return cn.psvmc.cxzapi.response.Result
    */
    @PostMapping("/add")
    public Result add(@RequestBody TUser tUser) {
        ToolUtils.validation(new Object[]{
                tUser.getUsertype(), "用户类型", 1
                , tUser.getRoleIds(), "角色id", 3
                , tUser.getUserphone(), "手机号", 2
        });
        if (ToolUtils.checkPhone(tUser.getUserphone())) {
            return ResultGenerator.genFailResult(ResultCode.PHONE_ERROR);
        }
        if (tUser.getUsertype().equals(2) && !ToolUtils.isTrueInteger(tUser.getStoreid())) {
            return ResultGenerator.genFailResult("商家不能为空！");
        }
        tUserService.add(tUser);
        return ResultGenerator.genSuccessResult("保存成功！");
    }

    /**
    * @Author auto 2024-08-08 更新用户表
    * @Param tUser 用户表模型
    * @Return cn.psvmc.cxzapi.response.Result
    */
    @PostMapping("/edit")
    public Result edit(@RequestBody TUser tUser) {
        ToolUtils.validation(new Object[]{tUser.getUserid(), "id", 1, tUser.getRversion(), "版本", 1});
        int updateResult = tUserService.edit(tUser);
        if (updateResult > 0) {
            return ResultGenerator.genSuccessResult("更新成功！");
        }
        return ResultGenerator.genFailResult("更新失败！");
    }

    /**
    * @Author auto 2024-08-08 查询用户表详情
    * @Param tUser
    * @Return cn.psvmc.cxzapi.response.Result
    */
    @PostMapping("/detail")
    public Result detail(@RequestBody TUser tUser) {
        Integer id = tUser.getUserid();
        ToolUtils.validation(new Object[]{id, "id", 1});
        return ResultGenerator.genSuccessResult("查询成功", tUserService.detail(id));
    }

    /**
    * @Author auto 2024-08-08 删除用户表
    * @Param tUser
    * @Return cn.psvmc.cxzapi.response.Result
    */
    @PostMapping("/delete")
    public Result delete(@RequestBody DelEntity tUser) {
        Integer id = tUser.getId();
        Integer rversion = tUser.getRversion();
        ToolUtils.validation(new Object[]{id, "id", 1, rversion, "版本", 1});
        int delResult = tUserService.delete(id, rversion);
        if (delResult > 0) {
            return ResultGenerator.genSuccessResult("删除成功！");
        }
        return ResultGenerator.genFailResult("删除失败！");
    }

    /**
    * @Author auto 2024-08-08 查询用户表列表
    * @Param entity 分页查询通用实体类
    * @Return cn.psvmc.cxzapi.response.Result
    */
    @PostMapping("/list")
    public PageResult2 list(@RequestBody SearchEntity entity) {
        Page<TUser> queryPage = new Page<>(entity.getPage(), entity.getSize());
        return PageUtil.getPageResult2(tUserService.list(queryPage, entity));
    }

    /**
     * @Description 商家用户列表
     * @Date 2024/8/20 11:25
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.PageResult2
     **/
    @PostMapping("/list_sj")
    public PageResult2 listSj(@RequestBody SearchEntity entity) {
        Page<TUser> queryPage = new Page<>(entity.getPage(), entity.getSize());
        return PageUtil.getPageResult2(tUserService.listSj(queryPage, entity));
    }

    /**
     * @Description 登录
     * @Date 2024/3/15 10:37
     * @Author YWX
     * @Param [user, request]
     * @Return com.xhkjedu.response.Result
     **/
    @PostMapping("/login")
    public Result loginByUserPhone(@RequestBody TUser user) {
        String userphone = ToolUtils.strTrim(user.getUserphone());
        String msgcode = ToolUtils.strTrim(user.getMsgcode());
        ToolUtils.validation(new Object[]{userphone, "手机号", 2, msgcode, "验证码", 2});
        user.setUserphone(userphone);
        user.setMsgcode(msgcode);
        return tUserService.loginByUserPhone(user);
    }

    /**
     * @Description 登录-切换用户
     * @Date 2024/8/14 23:22
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/select_store")
    public Result selectStore(@RequestBody TUser user) {
        Integer id = user.getUserid();
        ToolUtils.validation(new Object[]{id, "用户id", 1});
        return tUserService.selectStore(id);
    }

    /**
     * @Description 获取验证码
     * @Date 2024/8/10 22:02
     * @Author YWX
     * @Param [user]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/get_code")
    public Result getMsgCode(@RequestBody TUser user) {
        String userphone = ToolUtils.strTrim(user.getUserphone());
        ToolUtils.validation(new Object[]{userphone, "手机号", 2});
        return tUserService.getMsgCode(userphone);
    }

    /**
     * @Description 解锁
     * @Date 2025/2/21 22:14
     * @Author YWX
     * @Param [user]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/unlock")
    public Result unLock(@RequestBody TUser user) {
        String userphone = ToolUtils.strTrim(user.getUserphone());
        ToolUtils.validation(new Object[]{userphone, "手机号", 2});
        tUserService.unLock(userphone);
        return ResultGenerator.genSuccessResult("解锁成功！");
    }

    /**
     * @Description 身份证识别
     * @Date 2024/8/10 22:56
     * @Author YWX
     * @Param [file]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/get_idcard")
    public Result getIdCard(@RequestParam("file") MultipartFile file) {
        return tUserService.getIdCard(file);
    }

    /**
     * @Description 重置密码
     * @Date 2024/8/13 12:47
     * @Author YWX
     * @Param [user]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/reset_pwd")
    public Result resetPwd(@RequestBody TUser user) {
        ToolUtils.validation(new Object[]{user.getUserid(), "用户id", 1});
        return tUserService.resetPwd(user);
    }

    /**
     * @Description 权限菜单
     * @Date 2024/8/13 15:54
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/list_menu")
    public Result listMenu() {
        return ResultGenerator.genSuccessResult(tUserService.listMenu());
    }

    /**
     * @Description 角色权限菜单
     * @Date 2025/1/22 22:48
     * @Author YWX
     * @Param []
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/list_role_menu")
    public Result listRoleMenu() {
        return ResultGenerator.genSuccessResult(tUserService.listRoleMenu());
    }
}
